package com.example.mongodb.mongodb;

import com.example.mongodb.entity.RestaurantInfo;
import com.example.mongodb.repository.DishesInfoRepository;
import com.example.mongodb.repository.RestaurantInfoRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author : zhayh
 * @date : 2021-5-16 19:41
 * @description :
 */

@Slf4j
@Service
public class DataQueueService {
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private DishesInfoRepository dishesInfoRepository;
    @Resource
    private RestaurantInfoRepository restaurantInfoRepository;

    public boolean sendCreateMessage(Long restaurantId) {
        try {
            rabbitTemplate.convertAndSend("exchange.restaurant",
                    "restaurant-create", restaurantId);
            log.info("数据同步队列1，新建餐厅： {}", restaurantId);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean sendUpdateMessage(Long restaurantId) {
        try {
            rabbitTemplate.convertAndSend("exchange.restaurant",
                    "restaurant-update", restaurantId);
            log.info("数据同步队列2，更新餐厅： {}", restaurantId);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean sendDeleteMessage(Long restaurantId) {
        try {
            rabbitTemplate.convertAndSend("exchange.restaurant",
                    "restaurant-delete", restaurantId);
            log.info("数据同步队列3，删除餐厅： {}", restaurantId);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据mysql中餐厅id，获取数据库中数据，组装为mongodb文档对象。
     *
     * @param restaurantId
     * @return
     */
    public RestaurantDoc getRestaurantDoc(Long restaurantId) {
        RestaurantDoc doc = new RestaurantDoc();
        Optional<RestaurantInfo> optional = restaurantInfoRepository.findById(restaurantId);
        if (optional.isPresent()) {
            RestaurantInfo restaurantInfo = optional.get();
            doc.setDataId(restaurantInfo.getId());
            //餐厅坐标解析
            List<Double> position = new ArrayList<>();
            position.add(restaurantInfo.getLon());
            position.add(restaurantInfo.getLat());
            doc.setPosition(position);
            doc.setDishesType(restaurantInfo.getDishesType());
            doc.setShopAvgPrice(restaurantInfo.getShopAvgPrice());
            doc.setShopContact(restaurantInfo.getShopContact());
            doc.setShopGrade(restaurantInfo.getShopGrade());
            doc.setShopMemo(restaurantInfo.getShopMemo());
            doc.setShopName(restaurantInfo.getShopName());
            doc.setShopSaleTime(restaurantInfo.getShopSaleTime());
            //获取餐厅关联的所有菜肴
            doc.setDishesList(dishesInfoRepository.findByRestaurantInfoId(restaurantId));
        }
        return doc;
    }


}
